package meshindexer.UMLSinterface;

import gov.nih.nlm.kss.UMLSKSServiceLocator;
import gov.nih.nlm.kss.UMLSKSServicePortType;
import gov.nih.nlm.kss.models.meta.concept.ConceptId;
import gov.nih.nlm.kss.models.meta.concept.ConceptIdGroup;
import gov.nih.nlm.kss.models.meta.meshentry.MeSHEntry;
import gov.nih.nlm.kss.models.meta.meshentry.MeSHEntryGroup;
import gov.nih.nlm.kss.query.meta.ConceptIdExactRequest;
import gov.nih.nlm.kss.query.meta.MeSHEntryRequest;
import java.net.MalformedURLException;
import java.net.URL;
import gov.nih.nlm.umlsks.authorization.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Client {

    private String username;
    private String password;
    private String umlsrelease;

    public Client() {
    }

    public String[] getMeshTerms(String searchString) {
        try {
            Vector<String> result = new Vector<String>();
            // Locate the authentication web service
            URL authURL = new URL("https://login.nlm.nih.gov:443/auth");
            AuthorizationPortType authPortType = new AuthorizationPortTypeServiceLocator().getAuthorizationPort(authURL);
            // Obtain a proxy granting ticket
            String pgt = authPortType.getProxyGrantTicket(username, password);
            // Obtain a single-use ticket
            String proxyTicket = authPortType.getProxyTicket(pgt, "http://umlsks.nlm.nih.gov");
            // Properties for UMLSKS web service
            String ksHost = "http://umlsks.nlm.nih.gov";
            String ksURI = ksHost + "/UMLSKS/services/UMLSKSService";
            // Locate the UMLSKS web service
            URL ksURL = new URL(ksURI);
            UMLSKSServicePortType umlsksService = new UMLSKSServiceLocator().getUMLSKSServicePort(ksURL);
            // Build the request object
            ConceptIdExactRequest idRequest = new ConceptIdExactRequest();
            idRequest.setCasTicket(proxyTicket);
            idRequest.setRelease(umlsrelease);
            idRequest.setSearchString(searchString);
            ConceptIdGroup idGroup = umlsksService.findCUIByExact(idRequest);
            Object[] contents = idGroup.getContents();
            for (int i = 0; i < contents.length; i++) {
                ConceptId cid = (ConceptId) contents[i];
                MeSHEntryRequest request = new MeSHEntryRequest();
                String newProxyTicket = authPortType.getProxyTicket(pgt, "http://umlsks.nlm.nih.gov");
                request.setCasTicket(newProxyTicket);
                request.setRelease(umlsrelease);
                request.setCUI(cid.getCUI());
                MeSHEntryGroup group = umlsksService.getMeSHEntries(request);
                for (int k = 0; k < group.getContents().length; k++) {
                    MeSHEntry en = (MeSHEntry) group.getContents()[k];
                    result.add(en.getSTR());
                }
            }
            String[] aux = new String[result.size()];
                for (int j = 0; j < aux.length; j++) {
                    aux[j] = (String) result.get(j);
            }
            return aux;
        } catch (Exception e) {
            return new String[0];
        }
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @param UMLSVersion the UMLSVersion to set
     */
    public void setUMLSVersion(String umlsrelease) {
        this.umlsrelease = umlsrelease;
    }
}
